#pragma once 
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

template<typename T>
class BlockQueue
{
  pthread_mutex_t lock;
  pthread_cond_t empty;
  pthread_cond_t full;

  public:
    BlockQueue()
    {}
    bool IsFull()
    {
      return cap == q.size(); 
    }

    bool IsEmpty()
    {

    }

    void Push(const T& in)
    {
      pthread_mutex_lock(&lock);
      while(IsFull()){
        pthread_cond_wait(&full, &lock);
      }
      q.push(in);
      if(q.size() >= cap/2){
        cout<<"数据很多，消费吧"<<endl;
        pthread_cond_signal(&empty);
      }
      pthread_mutex_unlock(&lock);
    }

    void Pop(T& out)
    {
      pthread_mutex_lock(&lock);
      while(IsEmpty()){
        pthread_cond_wait(&empty,&lock);
      }
      out = q.front();
      q.pop();
      if(q.size() <= cap/2){
        cout<<"空间很多，来消费"<<endl;
      }
      pthread_mutex_unlock(&lock);
    }

    ~BlockQueue()
    {
      pthread_mutex_destroy(&lock);
      pthread_cond_destroy(&full);
      pthread_cond_destroy(&empty);
    }
  private:
    std::queue<T> q;
    int cap;
};
